package com.qfedu.knowledge.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.qfedu.knowledge.entity.bo.FileInfoBO;
import com.qfedu.knowledge.entity.bo.QuestionInfoBO;
import com.qfedu.knowledge.entity.bo.UserBO;
import com.qfedu.knowledge.entity.dto.ImportQuestionDTO;
import com.qfedu.knowledge.factory.FileFactory;
import com.qfedu.knowledge.service.IFileService;
import com.qfedu.knowledge.service.IQuestionInfoService;
import com.qfedu.knowledge.service.IUploadService;
import com.qfedu.knowledge.service.IUserService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UploadServiceImpl
 * @Author ykx
 * @Date 2024/5/1 18:35
 * @Version 1.0
 */
@Service
@Slf4j
public class UploadServiceImpl implements IUploadService {

    @Autowired
    private IFileService fileService;

    @Autowired
    private FileFactory fileFactory;

    @Autowired
    private IQuestionInfoService questionInfoService;

    @Autowired
    private IUserService userService;

    @Override
    public FileInfoBO upload(MultipartFile multipartFile, Long questionId) throws IOException {
        // 存储附件信息至数据库
        FileInfoBO fileInfoBO = fileFactory.create(multipartFile);
        fileInfoBO.setQuestionId(questionId);

        fileService.save(fileInfoBO);
        return fileInfoBO;
    }

    @Override
    @SneakyThrows
    public String importQuestion(MultipartFile multipartFile) {
        InputStream inputStream = multipartFile.getInputStream();
        List<QuestionInfoBO> questionInfoBOS = new ArrayList<>();
        ExcelReaderBuilder read = EasyExcel.read(inputStream, ImportQuestionDTO.class, new AnalysisEventListener<ImportQuestionDTO>() {
            @Override
            public void invoke(ImportQuestionDTO importQuestionDTO, AnalysisContext analysisContext) {
                questionInfoBOS.add(QuestionInfoBO.BuildQuestion(importQuestionDTO));
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                questionInfoService.saveBatch(questionInfoBOS);
            }
        });
        //获取一个工作表
        ExcelReaderSheetBuilder sheet = read.sheet();
        //读取工作表内容:sheet方法参数：工作表的顺序号（从0开始）或者工作表的名字，不传默认为0
        sheet.doRead();
        return "导入成功";
    }

    @Override
    public FileInfoBO uploadImage(MultipartFile multipartFile, Long id) throws IOException {
        FileInfoBO fileInfoBO = fileFactory.create(multipartFile);
        UserBO userBO = userService.getById(id);
        userBO.setImageUrl(fileInfoBO.getUrl());
        userService.saveOrUpdate(userBO);
        return fileInfoBO;
    }
}
